package com.exam.user.controller;


import com.exam.user.api.entity.SysRole;
import com.exam.user.service.SysRoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/sysrole")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    @GetMapping("/listAll")
    public String listAll(Model model,@RequestParam(value = "page",defaultValue = "1")Integer pageNum,
                          @RequestParam(value = "size",defaultValue = "15")Integer pagesize) {
        model.addAttribute("subjectList",sysRoleService.getAll(pageNum,pagesize));

        return "subject_list";
    }


    @PostMapping("/addSub")
    public String addSub(SysRole sysRole){
         sysRoleService.addRole(sysRole);
         return "redirect:subject/listAll";
    }

    @GetMapping("/delSub/{id}")
    public String delSub(@PathVariable("id") Integer id){
        System.out.println(1);
        sysRoleService.deleteById(id);
        System.out.println(2);
        return "redirect:/subject/listAll";
    }

    @PutMapping("/editSub")
    public String editSub(SysRole sysRole){
       sysRoleService.updateById(sysRole);
        return "edit/listAll";
    }

    /**
     * 根据id查询一个科目
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @ResponseBody
    //@Cacheable(value = "user:login:cache", key = "#id")
    public String getSingle(@PathVariable("id") Integer id,Model model){
        model.addAttribute("subjectOne",sysRoleService.getSingle(id));
        return "subject_edit";
        //System.out.println("执行");
        //String subjectname=subjectService.getSingle(id).getSubjectName();
        //return subjectService.getSingle(id);
    }
}
